<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use think\response\Json;
use app\common\model\WebsiteConfig as WebsiteConfigModel;

class WebsiteConfig extends AdminBase
{
    public function lists(): string
    {
        View::assign(['configGroups' => WebsiteConfigModel::CONFIG_GROUP_MAP]);
        return View::fetch();
    }

    public function list(): Json
    {
        $page                  = $this->request->param('page', 1, 'intval');
        $limit                 = $this->request->param('limit', 10, 'intval');
        $name                  = $this->request->param('name', '', 'trim');
        $status                = $this->request->param('status', 0, 'intval');
        $configGroup           = $this->request->param('config_group', '', 'trim');
        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);

        $where = [];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }
        if ($configGroup) {
            $where[] = ['config_group', '=', $configGroup];
        }

        $adInfo = $websiteConfigBusiness->getWebsiteConfigList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $adInfo['rows'], $adInfo['count']);
    }

    public function add(): string
    {
        View::assign(['configGroups' => WebsiteConfigModel::CONFIG_GROUP_MAP]);
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $name        = $this->request->param('name', '', 'trim');
        $configGroup = $this->request->param('config_group', '', 'trim');
        $configCode  = $this->request->param('config_code', '', 'trim');
        $fieldType   = $this->request->param('field_type', '', 'trim');
        $sort        = $this->request->param('sort', '0', 'intval');
        $status      = $this->request->param('status', 0, 'intval');

        $data     = [
            'name'         => $name,
            'status'       => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'config_group' => $configGroup,
            'config_code'  => $configCode,
            'field_type'   => $fieldType,
            'sort'         => $sort,
            'website_code' => $this->websiteCode,
        ];
        $validate = (new \app\admin\validate\WebsiteConfig())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $res                   = $websiteConfigBusiness->addWebsiteConfig($this->cdnDomain, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $websiteConfig         = $websiteConfigBusiness->findWebsiteConfigById($id);
        if (empty($websiteConfig)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(['configGroups' => WebsiteConfigModel::CONFIG_GROUP_MAP, 'websiteConfig' => $websiteConfig]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id          = $this->request->param('id', 0, 'intval');
        $name        = $this->request->param('name', '', 'trim');
        $configGroup = $this->request->param('config_group', '', 'trim');
        $configCode  = $this->request->param('config_code', '', 'trim');
        $fieldType   = $this->request->param('field_type', '', 'trim');
        $sort        = $this->request->param('sort', '0', 'intval');
        $status      = $this->request->param('status', 0, 'intval');

        $data     = [
            'name'         => $name,
            'status'       => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'config_group' => $configGroup,
            'config_code'  => $configCode,
            'field_type'   => $fieldType,
            'sort'         => $sort,
        ];
        $validate = (new \app\admin\validate\WebsiteConfig())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $res                   = $websiteConfigBusiness->updateWebsiteConfig($this->cdnDomain, $id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $res                   = $websiteConfigBusiness->deleteWebsiteConfig($this->cdnDomain, $id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $data = [];
        foreach ($idArr as $k => $v) {
            $data[$v] = $sortArr[$k];
        }

        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $res                   = $websiteConfigBusiness->batchWebsiteConfigSort($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
    }

    public function setting(): string
    {
        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $websiteConfigInfo     = $websiteConfigBusiness->getWebsiteConfigInfo($this->cdnDomain);
        View::assign(["websiteConfigInfo" => $websiteConfigInfo]);
        return View::fetch();
    }

    public function doSetting(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $fields = $this->request->param();
        $data   = [];
        foreach ($fields as $key => $value) {
            if ($key == "file") {
                continue;
            }
            $data[] = [
                'config_code'  => $key,
                'config_value' => $value,
            ];
        }

        $websiteConfigBusiness = new \app\admin\business\WebsiteConfig($this->websiteCode);
        $res                   = $websiteConfigBusiness->batchUpdateWebsiteConfigByCode($this->cdnDomain, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }
        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

}
